package cn.donting.jme.edit.attribute;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;

import java.io.IOException;

public class BooleanAttribute extends Attribute<Boolean> {

    private CheckBox checkBox=new CheckBox();

    public BooleanAttribute(String title) {
        super(title);
        checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                setSuperValue(newValue);
            }
        });
    }
    public BooleanAttribute() {
        this("");
    }

    @Override
    public void setValue(Boolean value) {
        checkBox.setSelected(value);
    }

    private void setSuperValue(Boolean value) {
        super.setValue(value);
    }

    @Override
    public Node getUINode() {
       return checkBox;
    }

    @Override
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
        OutputCapsule capsule = ex.getCapsule(this);
        capsule.write(getValue(),"value",false);
    }

    @Override
    public void read(JmeImporter im) throws IOException {
        super.read(im);
        InputCapsule capsule = im.getCapsule(this);
        setValue(capsule.readBoolean("value",false));
    }
}
